<script>
import HuilanUploader from "@/components/uploader/index.vue";
import {changeStatus, deleteBackground, getBackPageList} from "@/api/aihuman/humanAssets/backgroundGallery";
import AddVideo from "@/views/aihuman/HumanAssets/videoGallery/AddVideo.vue";
import {deleteVideoBatch, getVideoPageList} from "@/api/aihuman/humanAssets/videoGallery";

export default {
  name: "VideoList",
  components: {AddVideo, HuilanUploader},
  data() {
    //检验声音id
    var checkType = (rule, value, callback) => {
      // var inputType = /[^\w]/g
      var inputReg = /[a-zA-Z0-9-_]/g;
      if (!inputReg.test(value)) {
        callback(new Error('声音id由数字、英文、短划线、下划线组成'))
      } else {
        callback()
      }
    };
    return {
      groupId: '',
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      form: {},
      audioData: '',
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100],
      },
      tableData: [],
      tableOptions: {
        //新增按钮
        addBtn: false,
        header: false,
        viewBtn: false,
        delBtn: false,
        editBtn: false,
        refreshBtn: false,
        columnBtn: false,
        searchShowBtn: false,
        stripe: false,
        selection: true,
        tip: false,
        // simplePage: false,
        searchMenuSpan: 8,
        border: true,
        index: true,
        indexWidth: 60,
        indexLabel: "序号",
        menuWidth: 252,
        align: "left",
        menuAlign: "left",
        dialogWidth: "40%",
        dialogClickModal: false,
        dialogType: "drawer",
        span: 24,
        dialogCustomClass: "huilan-drawer-form",
        emptyText: this.$t('aicosCommon.notData'),
        column: [
          {
            label: "视频名称",
            prop: "name",
            minWidth: 200,
            // search: true,
            searchSpan: 8,
            overHidden: true
          },
          {
            label: "视频id",
            prop: "id",
            minWidth: 200,
            // search: true,
            searchSpan: 8,
            overHidden: true
          },
          {
            label: "尺寸",
            prop: "orientation",
            minWidth: 200,
            // search: true,
            searchSpan: 8,
            overHidden: true,
            dicData: [
              {
                label: '横屏16:9',
                value: 1
              },
              {
                label: '竖屏9:16',
                value: 2
              }
            ]
          },
          {
            label: '视频预览',
            prop: '',
            minWidth: 200,
            formatter: val => {
              return  `<video controls src="${window.location.origin}/${val.previewUrl}" style="width: 200px;"></video>`
            }
          },
          {
            label: "状态",
            prop: "status",
            type: "switch",
            dicData: [
              {
                label: "启用",
                value: 0,
              },
              {
                label: "禁用",
                value: 1,
              },
            ],
          },
          {
            label: "创建时间",
            minWidth: 150,
            prop: "createTime",
            overHidden: true,
            display: false
          }
        ],
      },
      searchForm: {
      },
      searchFieldList: [
        {
          prop: 'name',
          placeholder: '请输入视频名称'
        },
        {
          prop: 'id',
          placeholder: '请输入id'
        }
      ],
      selectedList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleAdd() {
      this.$refs.addVideo.open('add');
    },
    currentChange(current) {
      this.page.currentPage = current
      this.getList()
    },
    sizeChange(size) {
      this.page.pageSize = size
      this.getList()
    },
    configColumn() {
      this.$refs.listRef.$refs.dialogColumn.columnBox = true;
    },
    searchFn() {
      this.page.currentPage = 1
      this.getList()
    },
    getList() {
      this.$nextTick(async () => {
        let params = {
          ...this.searchForm,
          pageNo: this.page.currentPage,
          pageSize: this.page.pageSize
        }
        const {data} = await getVideoPageList(params)
        this.tableData = data.data.records
        this.page.total = data.data.total
      })
    },
    selectionChange(selection) {
      this.selectedList = selection
    },
    async batchRemove() {
      if (this.selectedList.length === 0) {
        this.$message({
          type: "warning",
          message: "请选择需要处理的数据",
        });
        return false
      }

      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let ids = this.selectedList.map(item => item.id)

        let params = {
          ids: ids.join(",")
        }
        const {data} = await deleteVideoBatch(params)
        this.$message({
          type: 'success',
          message: data.msg
        })
        this.selectedList = []
        this.getList()
      });

    },
    removeData(row) {
      this.selectedList.push(row)
      this.batchRemove()
    },
    async toggleAction(row) {
      const {data} = await changeStatus({id: row.id, status: row.status ^ 1})
      this.getList()
    },
    previewRow(row) {
      this.$refs.addVideo.open('preview', row)
    },
    editRow(row) {
      this.$refs.addVideo.open('edit', row)
    },
    // async uploadAudio(file) {
    //   console.log(file);
    //   let formData = new FormData()
    //   formData.append('file', file.file)
    //   formData.append('fromSystemName', 'aimeta')
    //
    //   const {data} = await uploadSound(formData)
    //   this.audioData = data.data.url
    //   this.$refs.soundAudio.load()
    // },
    // saveSound(form, done, loading) {
    //   console.log(form, done, loading);
    // }
  }
}
</script>

<template>
  <div style="padding: 5px 30px 0">
    <HuilanBasicToolbar style="margin-top: 0">
      <el-button type="primary" size="small" @click="handleAdd">{{
          $t("aicosCommon.addBtn")
        }}
      </el-button>

      <el-button type="danger" size="small" @click="batchRemove">
        批量删除
      </el-button>
    </HuilanBasicToolbar>

    <HuilanBasicSearchbar :model="searchForm" :fields="searchFieldList" @search="searchFn" @reset="getList">
      <template v-slot:appendTool>
        <el-button type="default" @click="configColumn"><i class="el-icon-s-operation"></i></el-button>
      </template>
    </HuilanBasicSearchbar>
    <avue-crud
      ref="listRef"
      v-model="form"
      :option="tableOptions"
      :page.sync="page"
      :data="tableData"
      @current-change="currentChange"
      @size-change="sizeChange"
      @selection-change="selectionChange"
    >
      <template slot-scope="scope" slot="menu">
        <el-button type="text" @click="previewRow(scope.row)">查看</el-button>
        <el-button type="text" @click="editRow(scope.row)">编辑</el-button>
        <el-button type="text" @click="toggleAction(scope.row)">
          {{ scope.row.status === 0 ? '停用' : '启用' }}
        </el-button>
        <el-button type="text" @click="removeData(scope.row)">删除</el-button>
      </template>
    </avue-crud>

    <add-video
      ref="addVideo"
      @save="getList"
    />
  </div>
</template>

<style scoped lang="scss">
.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
